﻿//用于商家翻译页面添加、修改、删除、查询

var $dataAddTemplate = null;
var $dataListTemplate = null;
var $tbodyAdd = null;
var $tbodyList = null;
var ajaxUrl = "Handler.ashx";

//DOM樹加載完成
$(function () {
    $dataAddTemplate = $("#dataAddTemplate");
    $dataListTemplate = $("#dataListTemplate");
    $tbodyAdd = $("#tbodyAdd");
    $tbodyList = $("#tbodyList");

    addEmptyRow(5);//添加空行
    getTranslatePageList();//获取翻译页面
});

var pageIndex = 0;//頁索引

//添加空行
//rowCount 行數
function addEmptyRow(rowCount) {
    var datas = [];
    for (var i = 1; i <= rowCount; i++) {
        var data = {};
        data.PageId = ++pageIndex;
        data.Name = "";
        data.Url = "";
        datas.push(data);
    }

    var html = $.templates($dataAddTemplate.text()).render(datas);
    $tbodyAdd.append(html);
}

//是否存在翻译页面名称或页面地址
function isExistsTranslatePageNameOrUrl(pageId, isName, obj_name_url)
{
    var name_url = $(obj_name_url).val();
    if (name_url == "")
    {
        return;
    }

    var data = {
        "Operate": "IsExistsTranslatePageNameOrUrl"
    };

    if (isName)
    {
        data.Name = name_url;
    }
    else {
        data.Url = name_url;
    }
    
    if (isName) {
        msgBox.showMsgWait(txt_CheckPageName); //显示消息面板
    }
    else {
        msgBox.showMsgWait(txt_checkPageUrl); //显示消息面板
    }

    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            if (isName){
                msgBox.showMsgOk(txt_PageNameUsed); //顯示消息面板
            }
            else {
                msgBox.showMsgOk(txt_PageUrlUsed); //顯示消息面板
            }
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
            obj_name_url.focus();
        }
    }, "json");
}

//添加所有翻譯頁面
function addAllTranslatePage()
{
    var $form = $("#formAdd");
    var data = $form.serialize();
    data += "&Operate=AddAllTranslatePage&PageCount=" + pageIndex;

    msgBox.showMsgWait(txt_Save); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            var $tr = null;
            var strI = null;
            $tbodyAdd.children().each(function (i) {
                strI = ++i;
                $tr = $(this);
                $("#txtName_" + strI, $tr).val("");
                $("#txtUrl_" + strI, $tr).val("");
            });

            getTranslatePageList();
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var isFirstLoad=true;//是否是首次加載數據
var sortField = null;
var isDesc = null;

//獲取排序字段和方法
function getSortInfo()
{
    sortField = "PageId";
    isDesc = false;
    return;
}

//获取翻譯頁面列表
function getTranslatePageList() {
    var keyword=$("#txtKeyword").val();
    getSortInfo();//獲取排序信息
    var keyword = $("#txtKeyword").val();
    var data={
        "Operate": "GetTranslatePageList",
        "SortField":sortField,
        "IsDesc":isDesc,
        "Keyword":keyword
    };

    msgBox.showMsgWait(txt_Loading); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            if (ajaxMsg.data.length > 0) {//有数据
                var html = $.templates($dataListTemplate.text()).render(ajaxMsg.data);
                $tbodyList.html(html);
            }
            else {
                $tbodyList.children().remove();
                $tbodyList.html("<tr><td colspan=" + $tbodyList.attr("colspan") + ">"+txt_NoData+"</td></tr>");
            }
            msgBox.hidBox(); //隐藏消息面板
            return;//以下为分页代码，暂时没用

            var colspan=$tbody.prev().children().eq(0).children().length;
            if ($tbodyList.children().length > 0) {//如果已有数据，先移去
                $tbodyList.children().remove();
            }

            if(ajaxMsg.data.length>0)
            {
                $dataListTemplate.tmpl(ajaxMsg).appendTo($tbody); //添加数据

                $('#pagination-demo').twbsPagination({//分頁
                    totalPages: ajaxMsg.totalPages,
                    visiblePages: 10,
                    onPageClick: function (event, page) {
                        if(!isFirstLoad)
                        {                                    
                            getFiledList(page);
                        }
                        isFirstLoad=false;
                    },
                    first: txt_FirstPage,
                    prev: txt_PreviousPage,
                    next: txt_NextPage,
                    last: txt_LastPage
                });
            }
            else{
                $("<tr><td colspan='"+colspan+"'>"+txt_NoData+"</td></tr>").appendTo($tbody);
            }
            msgBox.hidBox(); //隐藏消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");
}

var name = null;
var url = null;

//修改頁面
//btnSave 當前點擊的保存按鈕
function editTranslatePage(btnEdit)
{
    var strPageId = $(btnEdit).attr("id").replace("btnEdit_", "");
    if (isNaN(strPageId)) {
        alert(txt_Error);
        return;
    }

    var pageId = parseInt(strPageId);
    if (pageId == 0) {
        alert(txt_Error);
        return;
    }

    var $tr = $("#tr_" + pageId, $tbodyList);
    var $txtName = $("#txtName_" + pageId, $tr);
    var $txtUrl = $("#txtUrl_" + pageId, $tr);

    if ($txtName.length > 0 && $txtUrl.length > 0)
    {
        if ($txtName.attr("id") != "txtName_" + strPageId || $txtName.attr("id").replace("txtName_", "") != $txtUrl.attr("id").replace("txtUrl_", ""))
        {
            alert(txt_Error);
            return;
        }

        name = $txtName.val();
        url = $txtUrl.val();
        var data = {
            "Operate": "EditTranslatePage",
            "PageId": pageId,
            "Name": name,
            "Url": url
        };

        msgBox.showMsgWait(txt_TryingToModify); //显示消息面板
        $.post(ajaxUrl, data, function (ajaxMsg) {
            if (ajaxMsg.status == 1) {
                msgBox.showMsgOk(txt_Success); //顯示消息面板
            }
            else {
                msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
                $txtName.focus();
            }
        }, "json");

        return;
    }
    else
    {
        alert(txt_Error);
        return;
    }
}

//刪除頁面
//btnDelete 當前點擊的刪除按鈕
function deleteTranslatePage(btnDelete)
{
    if (confirm(txt_SureDelete) == false) {
        return;
    }

    var strPageId = $(btnDelete).attr("id").replace("btnDelete_", "");
    if (isNaN(strPageId)) {
        alert(txt_Error);
        return;
    }

    var pageId = parseInt(strPageId);
    if (pageId <= 0) {
        alert(txt_Error);
        return;
    }

    var $tr = $("#tr_" + pageId,$tbodyList);
    var data = {
        "Operate": "DeleteTranslatePage",
        "PageId": pageId
    };

    msgBox.showMsgWait(txt_TryingToDelete); //显示消息面板
    $.post(ajaxUrl, data, function (ajaxMsg) {
        if (ajaxMsg.status == 1) {
            $tr.remove();
            msgBox.showMsgOk(txt_Success); //顯示消息面板
        }
        else {
            msgBox.showMsgErr(ajaxMsg.msg); //显示消息面板
        }
    }, "json");

    return;
}